package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.JurisdictionManagementDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.PermissionTime;
import com.itheima.service.JurisdictionManagementService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service(interfaceClass = JurisdictionManagementService.class)
@Transactional
public class JurisdictionManagementServiceImpl implements JurisdictionManagementService{

    @Autowired
    private JurisdictionManagementDao jurisdictionManagementDao;

    //权限管理分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        //使用Mybatis框架提供的分页功能
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //调用Dao层进行查询结果
        if ("".equals(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString(null);
        }
        Page<PermissionTime> page =  jurisdictionManagementDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增 或 编辑修改权限
    @Override
    public Result add(PermissionTime permissionTime) {

        //根据id进行查询权限判断是修改还是添加
        PermissionTime permissionTime2 = this.echoJurisdiction(permissionTime.getId());
        //查询为空添加,不为空修改
        if(permissionTime2 == null){
            //根据权限名查询对应权限名是否存在 如存在直接返回return
            PermissionTime permissionTime1 =  jurisdictionManagementDao.jurisdictionAll(permissionTime.getName());
            if(permissionTime1 != null){
                return new Result(false,"当前权限名已存在,请重新添加");
            }
            //根据传入的关键字查询对应关键字是否存在
            PermissionTime permissionTime3 = jurisdictionManagementDao.allKeyword(permissionTime.getKeyword());
            if(permissionTime3 != null){
                return new Result(false,"当前关键字已存在,请重新添加");
            }
            //创建当前时间赋值到对应实体类中 将当前时间添加到对应权限关联id表中
            permissionTime.setCreation_time(new Date());
            //权限表进行添加权限
            jurisdictionManagementDao.jurisdictionAdd(permissionTime);

            permissionTime.setId(permissionTime.getId());
            //权限表对应的关联id时间表进行添加
            jurisdictionManagementDao.jurisdictionRelevanceAdd(permissionTime);
            return new Result(true,"权限添加成功");
        }else{
            //根据权限名查询对应权限名是否存在 如存在直接返回return
            PermissionTime permissionTime1 = jurisdictionManagementDao.jurisdictionAll(permissionTime.getName());
            if(permissionTime1 != null && permissionTime1.getId() != permissionTime.getId()){
                return new Result(false,"当前权限名已存在,请重新编辑");
            }
            //根据传入的关键字查询对应关键字是否存在
            PermissionTime permissionTime3 = jurisdictionManagementDao.allKeyword(permissionTime.getKeyword());
            if(permissionTime3 != null && permissionTime3.getId() != permissionTime.getId()){
                return new Result(false,"当前关键字已存在,请重新编辑");
            }
            //权限表进行修改
            jurisdictionManagementDao.amendJurisdiction(permissionTime);
            //关联的中间表进行修改
            jurisdictionManagementDao.relevanceMiddleJurisdiction(permissionTime);
            return new Result(true,"权限修改成功");
        }
    }

    //编辑回显权限信息
    @Override
    public PermissionTime echoJurisdiction(Integer id) {

        return jurisdictionManagementDao.echoJurisdiction(id);
    }


    //删除权限
    @Override
    public Result deleteJurisdiction(Integer id) {

        try {
            //根据传入的权限id删除权限关联的中间表
            jurisdictionManagementDao.deleteMiddleJurisdiction(id);
            //根据传入的权限id删除对应权限
            jurisdictionManagementDao.deleteJurisdiction(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
}
